
#include <string.h>
#include <stdio.h>
#include"mhscpu.h"
#include "ring_buffer.h"


RingBufferTypeDef ringbuf;

uint8_t tmpRece = 0;
uint32_t flagRece = 0;
uint32_t flagSend = 0;


void NVIC_Configuration(void);
void UART_Configuration(void);
uint8_t u8USARTGetDate[1024] = {0};
uint32_t u32USARTGetDateIndex = 0;
uint32_t u32USARTGetDateNum = 256;

UART_TypeDef *UARTx = UART0;

int main(void)
{
	int32_t tmpSend;
	ringBufferInit(&ringbuf);
	
	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_UART1 |  SYSCTRL_APBPeriph_GPIO,ENABLE);
	SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0 | SYSCTRL_APBPeriph_UART1 , ENABLE);
	
	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3, GPIO_Remap_0);
	GPIO_PinRemapConfig(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15, GPIO_Remap_3);
	
	NVIC_Configuration();
	UART_Configuration();
	
	printf("MegaHunt SCPU UART Demo V1.0.\n");
	
	UART_SetRTS(UARTx);
	while(1) 
	{
		
		if(ringbuf.count >= 255)
		{	
			while(ringbuf.count > 0)
			{
				if(UARTx->MSR & UART_MODEM_STATUS_CTS)
				{
					tmpSend = ringBufferGet(&ringbuf);
					while(!UART_IsTXEmpty(UARTx));
					UART_SendData(UARTx, (uint8_t)tmpSend);
				}
			}
			UART_SetRTS(UARTx);
		}
	}
}

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

	NVIC_InitStructure.NVIC_IRQChannel = UART0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
	NVIC_Init(&NVIC_InitStructure);
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;
	UART_FIFOInitTypeDef UART_FIFOInitStruct;

	UART_StructInit(&UART_InitStructure);
	UART_FIFOStructInit(&UART_FIFOInitStruct);
	
	UART_InitStructure.UART_BaudRate = 115200;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;
	
	
	UART_FIFOInitStruct.FIFO_Enable = ENABLE;
	UART_FIFOInitStruct.FIFO_DMA_Mode = UART_FIFO_DMA_Mode_1;
	UART_FIFOInitStruct.FIFO_RX_Trigger = UART_FIFO_RX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_Trigger = UART_FIFO_TX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_TriggerIntEnable = ENABLE;

	UART_Init(UART0, &UART_InitStructure);
	UART_Init(UART1, &UART_InitStructure);

	UART_FIFOInit(UART0, &UART_FIFOInitStruct);
 	UART_ITConfig(UART0, UART_IT_RX_RECVD, ENABLE);
	
	UART_FIFOInit(UART1, &UART_FIFOInitStruct);
 	UART_ITConfig(UART1, UART_IT_RX_RECVD, ENABLE);
}

int SER_PutChar (int ch) {

  while(!UART_IsTXEmpty(UARTx));
	UART_SendData(UARTx, (uint8_t) ch);
  return ch;
}

int fputc(int c, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  if (c == '\n')  {
    SER_PutChar('\r');
  }
  return (SER_PutChar(c));
}



#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif






